<?php
include("header.php");

$classID = $_GET["id"];
$info = array();
$sections = array();

//query gets one line of class information, no section or day data
$sql = "SELECT discipline.discName AS discipline, 
	college.collegeName AS college, 
	classes.courseNumber AS courseNumber, 
	classes_info.longName AS name, 
	classes_info.classDescrip AS descrip, 
	classes_info.bok AS bok, 
	classes_info.numCredits AS credits, 
	classes_info.isHonorsClass AS honors 
	FROM classes 
	JOIN discipline ON discipline.discID = classes.discipline_discID 
	JOIN college ON college.collegeID = discipline.college_collegeID 
	JOIN classes_info ON classes_info.classes_classID = classes.classID
	WHERE classes.classID = $classID";
	
$r = mysql_query($sql, $con);

if(mysql_num_rows($r)) {
	while($i = mysql_fetch_assoc($r)) {
		$info[] = $i;
	}
}

$sql = "SELECT sections.sectionNumber AS section,
	sections.callNumber AS callNumber,
	sections.flexDates AS notes,
	sections.instructor AS instructor,
	sections.location AS location,
	sections.sectionID AS sectionID
	FROM sections
	WHERE sections.classID = $classID";

$r = mysql_query($sql, $con);

if(mysql_num_rows($r)) {
	while($section = mysql_fetch_assoc($r)) {
		$sections[] = $section;
	}
}

foreach($sections as &$val){
	$sectionID = $val["sectionID"];
	$sql = "SELECT days.days AS day,
	days.startTime AS startTime,
	days.endTime AS endTime	
	FROM days
	WHERE days.sectionID = $sectionID";
	
	$r = mysql_query($sql, $con);
	$days = array();
	if(mysql_num_rows($r)) {
		while($day = mysql_fetch_assoc($r)) {
			$days[] = $day;
		}
	}
	$val["days"] = $days;
}

foreach($info as &$val){
	$val["sections"] = $sections;
}

header('Content-type: application/json');
echo json_encode($info);

include("footer.php");
?>